package com.partners.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.partners.dao.basic.FansDao;
import com.partners.dao.basic.UserDao;
import com.partners.dao.bo.FansDO;
import com.partners.dao.dto.UserInfoOutputDTO;
import com.partners.service.CustomerService;
import com.partners.service.FansService;
import com.partners.utils.JsonUtils;
import com.partners.utils.SessionManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liangyongle
 * @date 2018-12-04
 */
@Service
@Slf4j
public class FansServiceImpl implements FansService {


    @Resource
    private FansDao fansDao;

    @Resource
    private UserDao userDao;

    @Resource
    private CustomerService customerService;

    @Resource
    private SessionManager sessionManager;

    @Resource
    private HttpSession session;
    /**
     *根据openid查询粉丝信息
     *  @param  openId String
     * @return fanid Integer
    */
    @Override
    public Integer getFansByOpenId(String openId) {
        Integer result = fansDao.getFansByOpenId(openId);
        return result;
    }

    /**
     * 查询一条新的额粉丝信息
     *  @param  fans FansDO
     * @return fanid Integer
     */
    @Override
    public Integer InsertFans(FansDO fans) {
        int res = fansDao.insertSelective(fans);
        return fans.getFanid();
    }


    /**
     * 更新粉丝的信息
     *  @param  fansDO FansDO
     * @return fanid Integer
     */
    @Override
    public void UpdateFans(FansDO fansDO) {
        fansDao.updateByPrimaryKeySelective(fansDO);
    }


    /**
     * 保存缓存
     *  @param openId  String
     * @return fanid Integer
     */
    @Override
    public void setSessionFans(String openId) {
       Object fans = sessionManager.getCacheObject(openId);
        if(fans == null){
            fans = fansDao.getFansInfoByOpenId(openId);
            Integer overTime = 7200;
            sessionManager.setCacheObject(openId,fans,overTime);
        }
    }


    /**
     * 获取粉丝信息
     *  @param openId  String
     * @return fanid Integer
     */
    @Override
    public Object getFansInfoByOpenId(String openId) {
//        Object fans = sessionManager.getCacheObject(openId);
//        if(fans == null){
            Integer fansType = 0; //用户类型(默认是普通粉丝)
            FansDO fanInfo = fansDao.getFansInfoByOpenId(openId);
            if(fanInfo != null){
                Integer follow = fanInfo.getFollow().intValue();
                if(follow == 0){
                    return null;
                }
                Map<String,Object> detail = new HashMap<>();
                detail.put("fanid",fanInfo.getFanid());
                detail.put("type",fanInfo.getType());
                detail.put("openid",fanInfo.getOpenid());
                detail.put("nickname",fanInfo.getNickname());
                detail.put("avatar",fanInfo.getAvatar());
                detail.put("province",fanInfo.getProvince());
                detail.put("city",fanInfo.getCity());
                detail.put("dedist",fanInfo.getDedist());
                detail.put("address",fanInfo.getAddress());
                detail.put("follow",fanInfo.getFollow());
                detail.put("userId",fanInfo.getUserId());
                detail.put("sex",fanInfo.getSex());
                detail.put("addtime",fanInfo.getAddtime());
                detail.put("customer_id",fanInfo.getCustomerId());

                Integer userId = fanInfo.getUserId();
                Integer customerId = fanInfo.getCustomerId();
                //判断是否为用户
                if(userId != 0){
                    UserInfoOutputDTO user = userDao.getUserInfoById(userId);
                    if(user != null){
                        detail.put("user_name",user.getName());
                        detail.put("role_name",user.getRoleName());
                        detail.put("phone",user.getPhone());
                    }
                    fansType = 1; //用户
                }else if(customerId != 0){
                    fansType = 2; //客户
                }
                detail.put("fansType",fansType);
                Integer overTime = 7200;
                sessionManager.setCacheObject(openId,detail,overTime);
                return fanInfo;
            }else{
                return null;
            }

//        }else{
//            JSONObject jsonObject = JSON.parseObject(JsonUtils.toJson(fans));
//            Integer follow = jsonObject.getInteger("follow");
//            if(follow == 0){
//                return null;
//            }
//        }



    }


    /**
     * 扫描二维码得出的信息
     *  @param scene String
     * @return fanid Integer
     */
    @Override
    public void getSceneStr(String scene) {
        Object openid = session.getAttribute("openId");
        String[] splitstr = scene.split("&");
        String type = StringUtils.substringAfter(splitstr[0],"=");
        if(type.equals("1")){
            //用户扫码
            String uid = StringUtils.substringAfter(splitstr[1],"=");
            String id = StringUtils.substringAfter(splitstr[2],"=");

        }else if(type.equals("2")){
            //1查询是否已经领取过
            String uid = StringUtils.substringAfter(splitstr[1],"=");
            String customerId = StringUtils.substringAfter(splitstr[2],"=");
        }
        for(String res : splitstr){
           //System.out.println(res);
        }
        log.info("字符串{}",scene);
    }


}
